<?php

namespace App\Http\Controllers\Home;
use Gregwar\Captcha\CaptchaBuilder;
use Illuminate\Http\Request;
use App\Http\Requests\UserRegistRequest;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class MypageController extends Controller
{
	//解析模板 显示页面
	public function getPage()
	{
		// dd(22);
		$user=DB::table('users')->where('uid',session('uid'))->first();
		// dd($user);
		return view('home.mypage.mypage',['user'=>$user]);

	}

	//退出
	public function getOut()
	{

		//清除session
		session()->forget('uid');
		//重定向
		return view('home.login.login');
	}

	//显示用户信息修改页面
	public function getEdit()
	{
		$user=DB::table('users')->where('uid',session('uid'))->first();
		//解析模板
		return view('home.mypage.edit',['user'=>$user]);
	}

	

	//跳转到我的首页
	public function getMyshou(Request $request)
	{
		if(session('uid')){
			echo 1;
		}else{
			echo 0;
		}
	}

	//签到
	public function getQiandao(Request $request)
	{
		if(session('uid')){
			//现在时间
			$new=time();
			$res=DB::table('users')->where('uid',session('uid'))->first();
			//上次签到时间
			$before=$res->dotime;
			$time = $new-$before;
			if($time > 86400){
				$point=$res->userpoint+10;
				$result=DB::select('update users set userpoint ='.$point.', dotime ='.$new.' where uid='.session('uid'));
				
             	echo 1;
			}else{
				echo 2;
			}

		}else{
			echo 0;
		}
	}

	//我的积分
	public function getMypoint(Request $request)
	{
		if(session('uid')){
			$res=DB::table('users')->where('uid',session('uid'))->first();
			$res->userpoint;
			echo $res->userpoint;
		}else{
			echo 0;
		}
	}


	//我的空间
	public function getSpace(Request $request)
	{
		$user=DB::table('users')->where('uid',session('uid'))->first();
		$bid=DB::table('person')->where('uid',session('uid'))->get();

		$bname=[];
		foreach($bid as $k=>$v){
			// $bname[]=DB::table('block')->where('bid',$v->bid)->first()->bname;
			$bname[$k] = DB::select('select bname from block where bid = ?' , [$v->bid]);
			
		}
		// dd($bname);
		$bnames=[];
		foreach($bname as $k=>$v){
			foreach($v as $kk=>$vv){
				$bnames[]=$vv->bname;
			}
			
		}

		$goods=[];
		foreach($bid as $k=>$v){

			$goods[$k] = DB::select('select * from goods where gid = ?' , [$v->gid]);
			
		}
		$good=[];
		foreach($goods as $k=>$v){
			foreach($v as $kk=>$vv){
				$good[]=$vv;
			}			
		}
		// dd($good);
		return view('home.mypage.myspace',['user'=>$user,'bname'=>$bnames,'goods'=>$good]);
	}

	//显示个人资料修改页面
	public function getMydata()
	{
		$user=DB::table('users')->where('uid',session('uid'))->first();
		//解析模板
		return view('home.mypage.mydata',['user'=>$user]);
	}

	//个人资料修改
	public function postDataself(Request $request)
	{
		// dd($request->all());
		$data=$request->only(['sex','birth','address','team','signature','introduction']);
		//进行数据修改操作
		$res=DB::table('users')->where('uid',session('uid'))->update($data);
		if($res){
			return redirect('/home/mypage/space');
			// return view('home.mypage.myspace',['user'=>$user,'bname'=>$bnames]);
		}else{
			return back()->with('error','用户修改失败');
		}
	}

	

	/*
		用户的表单验证
	*/
	//处理ajax发送的用户名
	public function getCheck(Request $request)
	{
		
		$user=$request->all();
		//查询是否有用户
		$res=DB::table('users')->where('username',$user['username'])->first();

		// dd($res);
		if(empty($res)){
			echo 0;
		}else{
			if($res->uid==$user['uid']){
				echo 1;
			}else{
				echo 2;
			}		
		}
		
	}

	//处理ajax发送的手机号
	public function getPhone(Request $request)
	{
		$user=$request->all();
		//查询是否有用户
		$res=DB::table('users')->where('phone',$user['phone'])->first();

		if(empty($res)){
			echo 0;
		}else{
			if($res->uid==$user['uid']){
				echo 1;
			}else{
				echo 2;
			}		
		}
	}

	//处理ajax发送的Email
	public function getEmail(Request $request)
	{
		$user=$request->all();
		//查询是否有用户
		$res=DB::table('users')->where('email',$user['email'])->first();

		if(empty($res)){
			echo 0;
		}else{
			if($res->uid==$user['uid']){
				echo 1;
			}else{
				echo 2;
			}		
		}
	}


	//封装方法 进行头像上传处理
	private function upload($request,$filename)
	{
		if($request->hasFile($filename)){
			//获取文件后缀名
			$suffix=$request->file($filename)->getClientOriginalExtension();
			//随机文件名
			$name=md5(time().rand(1,9999));
			$request->file($filename)->move('./uploads/',$name.'.'.$suffix);
			return '/uploads/'.$name.'.'.$suffix;
		}
	}

	//进行数据修改
	public function postUpdate(Request $request)
	{
		// dd($request->all());
		if(empty($request->input('password'))){
			$data=$request->only(['username','email','phone']);	
		}else{
			$data=$request->only(['username','password','email','phone']);
			//密码处理 加密
			$data['password']=Hash::make($data['password']);
		}
				
		//判断是否有头像上传
		if($request->hasFile('pic')){
			$data['pic']=$this->upload($request,'pic');
			session(['pic'=>$data['pic']]);
		}
		// dd($data);
		//获取id
		$id=$request->input('uid');
		//进行数据修改操作
		$res=DB::table('users')->where('uid',$id)->update($data);
		if($res){
			//成功 到列表页
			$user=DB::table('users')->where('uid',session('uid'))->first();
			session(['username'=>$user->username]);
			return view('home.mypage.mypage',['user'=>$user]);
		}else{
			return back()->with('error','用户修改失败');
		}

	}	

	//我的发帖
	public function getPost(Request $request)
	{
		$user=DB::table('users')->where('uid',session('uid'))->first();
		// $post=DB::table('post')->where('puid',session('uid'))->get();
		$order=DB::select('select * from post order by ctr desc limit 10');

		$post=DB::select("select * from post LEFT JOIN block on post.bbid = block.bid LEFT JOIN users on post.puid = users.uid where users.uid = ".session('uid'));
				
		// dd($post);
		return view('home.mypage.mypost',['user'=>$user,'post'=>$post,'order'=>$order]);
	}

	//删除我的帖子
	public function getDelete(Request $request)
	{
		$post=$request->input('pid');

		$res=DB::table('post')->where('pid', $post)->delete();
		if($res==1){
			echo 1;
		}else{
			echo 0;
		}
	}


	//我的跟帖
	public function getFollow(Request $request)
	{
		$user=DB::table('users')->where('uid',session('uid'))->first();
		// $post=DB::table('post')->where('puid',session('uid'))->get();
		$order=DB::select('select * from post order by fnum desc limit 10');

		$follow=DB::select("select * from followup LEFT JOIN post on followup.fpid = post.pid LEFT JOIN block on  post.bbid = block.bid  LEFT JOIN users on followup.fuid = users.uid where users.uid = ".session('uid'));
				
		// dd($follow);
		return view('home.mypage.myfollow',['user'=>$user,'follow'=>$follow,'order'=>$order]);
	}

	//删除我的跟帖
	public function getFdel(Request $request)
	{
		$follow=$request->input('fid');

		$res=DB::table('followup')->where('fid', $follow)->delete();
		if($res==1){
			echo 1;
		}else{
			echo 0;
		}
	}


	//我的收藏
	public function getCollect(Request $request)
	{
		$user=DB::table('users')->where('uid',session('uid'))->first();
		// $post=DB::table('post')->where('puid',session('uid'))->get();
		$order=DB::select('select * from post order by ctr desc limit 10');
		
		$pid=DB::table('person')->where('uid',session('uid'))->get();
		// dd($pid);
		$coll=[];
		foreach($pid as $k=>$v){
			$coll[$k] = DB::select('select * from post where pid = ?' , [$v->pid]);
			
		}
		$colls=[];
		foreach($coll as $k=>$v){
			foreach($v as $kk=>$vv){
				$colls[]=$vv;
			}
			
		}
		// dd($colls);

		return view('home.mypage.mycollect',['user'=>$user,'order'=>$order,'coll'=>$colls]);
	}

}
